package com.example.jksj.bookmanagement.pro.mine.fragment;
/*
 *  项目名：    SharedRefrigerators
 *  包名：      com.sr.sharedrefrigerators.fragment
 *  文件名:     HomeFragment
 *  创建者:     HXD
 *  创建时间:   2018/11/25 12:13
 *  描述:       TODO
 */


import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.jksj.bookmanagement.R;
import com.example.jksj.bookmanagement.event.MessageEvent;
import com.example.jksj.bookmanagement.common.MyLazyFragment;
import com.example.jksj.bookmanagement.pro.mine.activity.LendHistoryActivity;
import com.example.jksj.bookmanagement.pro.mine.activity.LoginActivity;
import com.example.jksj.bookmanagement.pro.mine.activity.MyCollectionActivity;
import com.example.jksj.bookmanagement.pro.mine.activity.UserManageActivity;
import com.example.jksj.bookmanagement.pro.mine.activity.SettingActivity;
import com.example.jksj.bookmanagement.utils.L;
import com.example.jksj.bookmanagement.utils.ShareUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;

import de.hdodenhof.circleimageview.CircleImageView;

public class MineFragment extends MyLazyFragment implements View.OnClickListener {

    private CircleImageView ivPhoto;

    private TextView tvShowEmile;
    private TextView tvShowUserName;

    private Button btnCollection;
    private Button btnHistory;
    private Button btnEvaluate;
    private Button btnSetting;
    private Button btnExitLogin;


    public static MineFragment newInstance() {
        return new MineFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected int getTitleBarId() {
        return 0;
    }

    @Override
    public void initView() {
        ivPhoto = getView().findViewById(R.id.iv_photo_mine);
        ivPhoto.setVisibility(View.VISIBLE);
        ivPhoto.setOnClickListener(this);
        tvShowUserName = getView().findViewById(R.id.tv_ShowUserName_fgMine);
        tvShowEmile = getView().findViewById(R.id.tv_ShowEmail_fgMine);
        btnCollection = getView().findViewById(R.id.btn_collection_mine);
        btnCollection.setOnClickListener(this);
        btnHistory = getView().findViewById(R.id.btn_history_mine);
        btnHistory.setOnClickListener(this);
        btnEvaluate = getView().findViewById(R.id.btn_evaluate_mine);
        btnEvaluate.setOnClickListener(this);
        btnSetting = getView().findViewById(R.id.btn_setting_mine);
        btnSetting.setOnClickListener(this);
        btnExitLogin = getView().findViewById(R.id.bnt_ExitLogin_mine);
        btnExitLogin.setOnClickListener(this);
    }

    @Override
    public void initData() {

        //进行EventBus注册
        EventBus.getDefault().register(this);

        //缓存本地头像
        String imaePath = ShareUtils.getString("title_Image", "");
        if (!imaePath.equals("")){
            File file = new File(imaePath);
            Glide.with(this).load(file).into(ivPhoto);
            //L.i(imaePath);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_photo_mine:
                boolean isLogin = ShareUtils.getBoolean("isLogin", true);
                if (isLogin){
                    //如果登录了就打开账号管理界面
                    startActivity(UserManageActivity.class);
                }else {
                    //否则跳转到登录页
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;

            case R.id.btn_collection_mine:
                startActivity(new Intent(getActivity(), MyCollectionActivity.class));
                break;

            case R.id.btn_history_mine:
                startActivity(new Intent(getActivity(), LendHistoryActivity.class));
                break;

            case R.id.btn_evaluate_mine:
                break;

            case R.id.btn_setting_mine:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;

            case R.id.bnt_ExitLogin_mine:
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(MessageEvent messageEvent) {
        if (!(messageEvent.getMessage().equals(""))){
            File file = new File(messageEvent.getMessage());
            Glide.with(this).load(file).into(ivPhoto);
        }
        L.i("Event:" + messageEvent.getMessage());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

}
